<?php

class util {
	public static function isWicked($val) {
		$reg = '/[\'.,:;*?~`!@#$%^&+=)(<>{}]|\]|\[|\/|\\\|\"|\|/';
		return preg_match($reg, $val);
	}

	public static function getGet($field, $default = '') {
		return isset($_GET[$field]) ? util::mustFilter($_GET[$field]) : $default;
	}

	public static function mustFilter($val) {
		$ra = array
	    (
	        '/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/', '/script/', '/javascript/', '/vbscript/', 
	        '/expression/', '/applet/', '/meta/', '/xml/', '/blink/', '/link/', '/style/', '/embed/', 
	        '/object/', '/layer/', '/title/', '/bgsound/', '/base/', '/onload/', '/onunload/',
	        '/onchange/', '/onsubmit/', '/onreset/', '/onselect/', '/onblur/', '/onfocus/', '/onabort/', 
	        '/onkeydown/', '/onkeypress/', '/onkeyup/', '/onclick/', '/ondblclick/', '/onmousedown/', 
	        '/onmousemove/', '/onmouseout/', '/onmouseover/', '/onmouseup/', '/onunload/', '/delete/', 
	        '/insert/', '/update/', '/select/', '/\./', '/\//', '/\'/', '/\"/', '/\:/', '/\%/', '/\$/',
            '/\!/', '/\*/', '/\=/', '/\;/'
	    );
	    $val = preg_replace($ra, '', $val);
	    $val = htmlentities(strip_tags($val));
	    return $val;
	}

	public static function isMobileDevice() {
		if (isset($_SERVER['HTTP_X_WAP_PROFILE']))
	        return true;

	    if (isset($_SERVER['HTTP_VIA']) && stristr($_SERVER['HTTP_VIA'], "wap"))
	        return true;

	    $user_agent = $_SERVER['HTTP_USER_AGENT'];
	    $mobile_agents = array
	    (
	        'iphone','android','phone','mobile','wap','netfront','java','opera mobi',
	        'opera mini','ucweb','windows ce','symbian','series','webos','sony','blackberry','dopod',
	        'nokia','samsung','palmsource','xda','pieplus','meizu','midp','cldc','motorola','foma',
	        'docomo','up.browser','up.link','blazer','helio','hosin','huawei','novarra','coolpad',
	        'techfaith','alcatel','amoi','ktouch','nexian','ericsson','philips','sagem','wellcom',
	        'bunjalloo','maui','smartphone','iemobile','spice','bird','zte-','longcos','pantech',
	        'gionee','portalmmm','jig browser','hiptop','benq','haier','^lct','320x320','240x320',
	        '176x220','windows phone','cect','compal','ctl','lg','nec','tcl','daxian','dbtel','eastcom',
	        'konka','kejian','lenovo','mot','soutec','sgh','sed','capitel','panasonic','sonyericsson',
	        'sharp','panda','zte','acer','acoon','acs-','abacho','ahong','airness','anywhereyougo.com',
	        'applewebkit/525','applewebkit/532','asus','audio','au-mic','avantogo','becker','bilbo',
	        'bleu','cdm-','danger','elaine','eric','etouch','fly ','fly_','fly-','go.web','goodaccess',
	        'gradiente','grundig','hedy','hitachi','htc','hutchison','inno','ipad','ipaq','ipod',
	        'jbrowser','kddi','kgt','kwc','lg ','lg2','lg3','lg4','lg5','lg7','lg8','lg9','lg-','lge-',
	        'lge9','maemo','mercator','meridian','micromax','mini','mitsu','mmm','mmp','mobi','mot-',
	        'moto','nec-','newgen','nf-browser','nintendo','nitro','nook','obigo','palm','pg-',
	        'playstation','pocket','pt-','qc-','qtek','rover','sama','samu','sanyo','sch-','scooter',
	        'sec-','sendo','sgh-','siemens','sie-','softbank','sprint','spv','tablet','talkabout',
	        'tcl-','teleca','telit','tianyu','tim-','toshiba','tsm','utec','utstar','verykool','virgin',
	        'vk-','voda','voxtel','vx','wellco','wig browser','wii','wireless','xde','pad','gt-p1000'
	    );

	    $is_mobile = false;
	    foreach ($mobile_agents as $device) {
	        if (stristr($user_agent, $device)) {
	            $is_mobile = true;
	            break;
	        }
	    }
	    return $is_mobile;
	}

	public static function httpPost($url, $data = null) {
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_TIMEOUT, 500);
		curl_setopt($curl, CURLOPT_URL, $url);

		if (!empty($data)) {
			curl_setopt($curl, CURLOPT_POST, 1);
			curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
		}

		$res = curl_exec($curl);
		curl_close($curl);

		return $res;
	}

	public static function httpGet($url) {
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_TIMEOUT, 500);
		curl_setopt($curl, CURLOPT_URL, $url);

		$res = curl_exec($curl);
		curl_close($curl);

		return $res;
	}

	public static function setWxshare($title, $desc, $link, $imgUrl) {
		require_once('php/config/database.php');
		require_once('php/function/sqlcon.php');
		require_once('php/function/jssdk.php');

		$jssdk = new jssdk();
		$wxShare = $jssdk->getSignPackage();
		$wxShare['title'] = $title;
		$wxShare['desc'] = $desc;
		$wxShare['link'] = $link;
		$wxShare['imgUrl'] = $imgUrl;
		return $wxShare;
	}

	public static function getInfo($idField, $idValue, $table) {
		require_once('php/config/database.php');
		require_once('php/function/sqlcon.php');

		$rst = new sqlcon();
		$info = $rst->getInfo($idField . ', title', $table, $idField . " = '" . $idValue . "'");
		if (empty($info)) {
			return null;
		}

		//格式化列表小图路径（暂不用，改用Logo）
		/*if (!empty($info['smallpic'])) {
			$info['smallpic'] = _HTTP_PATH_ . $info['smallpic'];
		}
		else {
			$info['smallpic'] = _DOMAIN_ . 'static/pic/wx/logo.jpg';
		}*/

		return $info;
	}
}